package com.flyme.module.exam.provider.controller;


import com.flyme.common.core.model.ResultBody;
import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.core.base.supper.controller.BaseController;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.module.exam.client.api.ExamTypeRemoteService;
import com.flyme.module.exam.client.entity.ExamType;
import com.flyme.module.exam.provider.service.ExamTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author flyme
 */
@Slf4j
@Api(tags = "题目类型管理")
@RestController
public class ExamTypeController extends BaseController<ExamTypeService, ExamType> implements ExamTypeRemoteService {

    @ApiOperation(value = "获取题目分类")
    @Override
    public ResultBody tree(@ModelAttribute(value = "examType") ExamType examType) {
        CriteriaQuery qw = new CriteriaQuery<Map<String, Object>>();
        qw.select("id value", "CONCAT(examTypeName,'能力') label", "parentId", "isParent","sortOrder");
        qw.eq("parentId", examType.getParentId());
        qw.like("examTypeName",examType.getExamTypeName());
        List list = bizService.listMaps(qw);
        return ResultBody.success(toCascader(list));
    }

    @ApiOperation(value = "获取题目分类")
    @Override
    public ResultBody treeRank(@ModelAttribute(value = "examType") ExamType examType) {
        CriteriaQuery qw = new CriteriaQuery<Map<String, Object>>();
        qw.select("id value,parentId,isParent,sortOrder," +
            "case when examTypeCode = 'synthesis' then '能力综合测评指数' else CONCAT(examTypeName,'能力') end label");
        qw.eq("parentId", examType.getParentId());
        qw.or().eq("examTypeCode", "synthesis");
        List list = bizService.listMaps(qw);
        ExamType et = bizService.getOne(q().eq("examTypeCode", "synthesis"));
        Map<String, Object> map = new HashMap<>();

        return ResultBody.success(toCascader(list));
    }

    @ApiOperation(value = "添加题目分类", notes = "添加题目分类")
    @Override
    public ResultBody insert(@ModelAttribute(value = "examType") ExamType examType) {
        boolean result = bizService.insert(examType);
        return ResultBody.success(result);
    }

    @ApiOperation(value = "编辑题目分类", notes = "编辑题目分类")
    @Override
    public ResultBody edit(ExamType examType) {
        boolean result= bizService.update(examType);
        return ResultBody.success(result);
    }

    @ApiOperation(value = "删除题目分类", notes = "删除题目分类")
    @Override
    public ResultBody delById(@PathVariable Long id) {
        return bizService.delById(id);
    }

}
